package pers.qianyu.service.impl;

import io.seata.spring.annotation.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import pers.qianyu.dao.*;
import pers.qianyu.domain.*;
import pers.qianyu.service.*;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductService productService;

    @Override
    public void createOrder(Order order) {
        orderDao.save(order);
    }

    @GlobalTransactional
    @Override
    public Order createOrder(Integer pid) {
        Product product = productService.findById(pid);
        log.info("查询到商品号{}的商品信息：{}", pid, product.toString());

        Order order = new Order();
        order.setUid(1);
        order.setUsername("小雨");
        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        log.info("新建订单：{}", order.toString());

        // 扣库存
        productService.reduceInventory(pid,order.getNumber());

        return order;
    }
}